package com.longma.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.longma.server.entity.*;
import com.longma.server.pojo.vo.AlarmCountVO;
import com.longma.server.pojo.vo.home.*;
import com.longma.server.service.*;
import com.longma.server.util.AuthContextUtil;
import com.longma.server.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author HAIBO
 * @date 2021-05-10 10:00
 * @description 首页
 */
@Service
@Transactional
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    private IRealtimeDataService realtimeDataService;

    @Autowired
    private IAlarmRecordService alarmRecordService;

    @Autowired
    private IYuncangUserService yuncangUserService;

    @Autowired
    private IMonthDataService monthDataService;

    @Autowired
    private IDailyDataService dailyDataService;

    @Autowired
    private IYearDataService yearDataService;

    /**
     * 云仓状态信息
     */
    @Override
    public List<YuncangStateVO> getYuncangstate() {
        List<YuncangStateVO> res = new ArrayList<>();

        // 获取当前用户关联的云仓
        List<Yuncang> yuncangs = yuncangUserService.getYuncangs();
        if (CollUtil.isEmpty(yuncangs)){
            return res;
        }

        // 遍历
        for (Yuncang yuncang: yuncangs ) {
            res.add(new YuncangStateVO(yuncang.getId(),yuncang.getYuncangName(),yuncang.getLongitude(), yuncang.getLatitude(), yuncang.getState()));

        }
        return res;
    }

    /**
     * 发电量统计信息
     */
    @Override
    public PowerGenerationVO getPowerGeneration() {
        PowerGenerationVO res = new PowerGenerationVO();

        // 获取当前用户关联的云仓
        List<Yuncang> yuncangs = yuncangUserService.getYuncangs();
        List<Integer> yuncangIds = yuncangs.stream().map(Yuncang::getId).collect(Collectors.toList());
        if (CollUtil.isEmpty(yuncangIds)){
            return res;
        }

        double realTimePower =0;
        double dailyGeneration=0;
        double monthGeneration=0;
        double yearGeneration=0;
        double capacity=0;

        // 获取云仓最新数据
        Map<Integer,RealtimeData> yuncangDatas = realtimeDataService.getLatestRealtimeDataRecord(yuncangIds);

        // 遍历云仓
        for (Yuncang yuncang: yuncangs ) {
            Integer yuncangId = yuncang.getId();

            capacity += yuncang.getCapacity();

            // 获取云仓日数据
            DailyData dailyData = dailyDataService.getDailyData(yuncangId);
            dailyGeneration += dailyData.getGeneration();

            // 获取云仓月数据
            MonthData monthData = monthDataService.getMonthData(yuncangId);
            monthGeneration += monthData.getGeneration();

            // 获取云仓年数据
            YearData yearData = yearDataService.getYearData(yuncangId);
            yearGeneration += yearData.getGeneration();

            // 获取最近一条数据
            RealtimeData yuncangData = yuncangDatas.get(yuncangId);
            if (yuncangData == null){
                continue;
            }

            realTimePower += MathUtil.computeIfAbsent(yuncangData.getPvPower());
        }

        res.setRealTimePower(realTimePower)
                .setDailyGeneration(dailyGeneration)
                .setCount(yuncangs.size())
                .setCapacity(capacity)
                .setMonthGeneration(monthGeneration)
                .setYearGeneration(yearGeneration);

        return res;
    }

    /**
     * 用电量统计信息
     */
    @Override
    public PowerConsumptionVO getPowerConsumption(){
        PowerConsumptionVO res = new PowerConsumptionVO();

        // 获取当前用户关联的云仓
        List<Yuncang> yuncangs = yuncangUserService.getYuncangs();
        List<Integer> yuncangIds = yuncangs.stream().map(Yuncang::getId).collect(Collectors.toList());
        if (CollUtil.isEmpty(yuncangIds)){
            return res;
        }

        double realTimePower =0;
        double dailyConsumption=0;
        double monthConsumption=0;
        double yearConsumption=0;
        double capacity=0;

        // 获取云仓最新数据
        Map<Integer,RealtimeData> yuncangDatas = realtimeDataService.getLatestRealtimeDataRecord(yuncangIds);

        // 遍历云仓
        for (Yuncang yuncang: yuncangs ) {
            Integer yuncangId = yuncang.getId();
            capacity += yuncang.getCapacity();

            // 获取云仓日数据
            DailyData dailyData = dailyDataService.getDailyData(yuncangId);
            dailyConsumption += dailyData.getConsumption();

            // 获取云仓月数据
            MonthData monthData = monthDataService.getMonthData(yuncangId);
            monthConsumption += monthData.getConsumption();

            // 获取云仓年数据
            YearData yearData = yearDataService.getYearData(yuncangId);
            yearConsumption += yearData.getConsumption();

            // 获取最近一条数据
            RealtimeData yuncangData = yuncangDatas.get(yuncangId);
            if (yuncangData == null){
                continue;
            }
            realTimePower +=  MathUtil.computeIfAbsent(yuncangData.getUsedPower());
        }

        res.setRealTimePower(realTimePower)
                .setDailyConsumption(dailyConsumption)
                .setCount(yuncangs.size())
                .setCapacity(capacity)
                .setMonthConsumption(monthConsumption)
                .setYearConsumption(yearConsumption);

        return res;
    }

    /**
     * 获取告警数量
     */
    @Override
    public AlarmCountVO getAlarmCount() {
        return alarmRecordService.getAlarmCount(AuthContextUtil.getLoginName());
    }

    /**
     * 实时发电功率曲线
     */
    @Override
    public List<PointVO> getPowerGenerationCurve() {
        return realtimeDataService.getPowerGenerationCurve();
    }

    /**
     * 实时用电功率曲线
     */
    @Override
    public List<PointVO> getPowerConsumptionCurve() {
       return realtimeDataService.getPowerConsumptionCurve();
    }

    /**
     * 年发电量统计曲线
     */
    @Override
    public List<PointVO> getYearGenerationCurve() {
        return yearDataService.getYearGenerationCurve();
    }

    /**
     * 年用电量统计曲线
     */
    @Override
    public List<PointVO> getYearConsumptionCurve() {
        return yearDataService.getYearConsumptionCurve();
    }

    /**
     * 月用电量统计曲线
     */
    @Override
    public List<PointVO> getMonthConsumptionCurve() { return monthDataService.getMonthConsumptionCurve(); }

    /**
     * 月发电量统计曲线
     */
    @Override
    public List<PointVO> getMonthGenerationCurve() { return monthDataService.getMonthGenerationCurve(); }

}
